//
//  UIView+Categoty.m
//  PlayCatchGame
//
//  Created by 流言 on 2018/1/15.
//  Copyright © 2018年 清正. All rights reserved.
//

#import "UIView+Categoty.h"

@implementation UIView (Categoty)

- (void)setCornerRadius:(CGFloat)cornerRadius {
    self.layer.cornerRadius = cornerRadius;
    self.clipsToBounds = cornerRadius > 0;
}
- (CGFloat)cornerRadius {
    return self.layer.cornerRadius;
}

- (void)setCornerColor:(UIColor *)cornerColor {
    self.layer.borderColor = cornerColor.CGColor;
    self.layer.borderWidth =0.8;
}
- (UIColor *)cornerColor {
    return [UIColor colorWithCGColor:self.layer.borderColor];
}

- (void)cornerRectWith:(UIRectCorner)rectCorner cornerRadii:(CGSize)radii {
    UIBezierPath *maskPath  =[UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:rectCorner cornerRadii:radii];
    CAShapeLayer *maskLayer =[[CAShapeLayer alloc] init];
    maskLayer.frame =self.bounds;
    maskLayer.path =maskPath.CGPath;
    self.layer.mask =maskLayer;
}

- (void)shakeWithCount:(int)count interval:(NSTimeInterval)interval {
    NSDictionary *info = [NSDictionary dictionaryWithObjectsAndKeys:
                          [NSNumber numberWithDouble:interval], @"interval",
                          [NSNumber numberWithInt:count],@"count",
                          [NSValue valueWithCGPoint:self.center],@"center",
                          nil];
    [NSTimer scheduledTimerWithTimeInterval:interval
                                     target:self
                                   selector:@selector(shaking:)
                                   userInfo:info repeats:NO];
}

- (void)shaking:(NSTimer *)aTimer {
    NSMutableDictionary *info = [NSMutableDictionary dictionaryWithDictionary:[aTimer userInfo]];
    [aTimer invalidate];
    aTimer = nil;
    int shakeCnt = [[info objectForKey:@"count"] intValue];
    CGPoint orgCenter = [[info objectForKey:@"center"] CGPointValue];
    CGPoint to;
    if (shakeCnt % 2 == 0 && shakeCnt > 0) {
        to = CGPointMake(orgCenter.x + floor(random() % shakeCnt) - shakeCnt / 2,
                         orgCenter.y + floor(random() % shakeCnt) - shakeCnt / 2);
    } else {
        to = orgCenter;
    }
    
    [self setCenter:to];
    shakeCnt--;
    if (shakeCnt > 0) {
        [info setObject:[NSNumber numberWithInt:shakeCnt] forKey:@"count"];
        [NSTimer scheduledTimerWithTimeInterval:[[info objectForKey:@"interval"] doubleValue]
                                         target:self
                                       selector:@selector(shaking:)
                                       userInfo:info repeats:NO];
    }
}

- (void)startAnimation:(UIViewTransitionType)transitionType {
    CATransition *transition = [CATransition animation];
    transition.duration = 0.3f;
    transition.type = kCATransitionMoveIn;
    if (transitionType ==UIViewTransitionTypeBottomToTop) {
        transition.subtype = kCATransitionFromTop;
    } else if (transitionType ==UIViewTransitionTypeTopToBottom) {
        transition.subtype = kCATransitionFromBottom;
    } else if (transitionType ==UIViewTransitionTypeLeftToRight) {
        transition.subtype = kCATransitionFromRight;
    } else if (transitionType ==UIViewTransitionTypeRightToLeft) {
        transition.subtype = kCATransitionFromLeft;
    }
    [self.layer addAnimation:transition forKey:nil];
}

@end
